#pragma once
#include <iostream>
#include <string>
#include <unistd.h>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include"Log.hpp"
using namespace std;
enum{
    SocketErr = 2,
    BindErr,
    ListenErr,
};
const int backlog = 10;/////
class Sock
{
public:
    Sock()
    {
    }
    ~Sock()
    {   
    }
public:
    void Socket()
    {
        sockfd_=socket(AF_INET,SOCK_STREAM,0);
    }
    void Bind(uint16_t port)
    {
        struct sockaddr_in temp;
        temp.sin_family=AF_INET;
        temp.sin_port=htons(port);
        temp.sin_addr.s_addr=INADDR_ANY;
        if (bind(sockfd_, (struct sockaddr *)&temp, sizeof(temp)) < 0)
        {
            lg(Fatal, "bind error, %s: %d", strerror(errno), errno);
            exit(BindErr);
        }
    }
    void Listen()
    {
        if (listen(sockfd_, backlog) < 0)
        {
            lg(Fatal, "listen error, %s: %d", strerror(errno), errno);
            exit(ListenErr);
        }
    }
    bool Connect(const std::string &ip, const uint16_t &port)
    {
        struct sockaddr_in peer;
        memset(&peer, 0, sizeof(peer));
        peer.sin_family = AF_INET;
        peer.sin_port = htons(port);
        inet_pton(AF_INET, ip.c_str(), &(peer.sin_addr));

        int n = connect(sockfd_, (struct sockaddr*)&peer, sizeof(peer));
        if(n == -1) 
        {
            std::cerr << "connect to " << ip << ":" << port << " error" << std::endl;
            return false;
        }
        return true;
    }
    int Accept(string &clientip,uint16_t &port)
    {
        struct sockaddr_in peer;
        socklen_t len=sizeof(peer);
        int newfd=accept(sockfd_,(struct sockaddr*)&peer,&len);
        char ipstr[64];
        inet_ntop(AF_INET, &peer.sin_addr, ipstr, sizeof(ipstr));
        clientip = ipstr;
        port=ntohs(peer.sin_port);
        return newfd;
    }
    void Close()
    {
        close(sockfd_);
    }
    int Fd()
    {
        return sockfd_;
    }
private:
    int sockfd_;
};